<?php
/**
 * Modify by 		peace@warmy.cn
 * Modify Date		2009-03-11
 */
cache_delete_manage();
$cmd = isset($_GET['cmd'])? strtolower($_GET['cmd']) : 'list';
$warmy_tree			= new Warmy_tree($db,"",false,FUNCTIONTABLE);
switch($cmd)
{
	case "edit":
		Edit();
		break;
	case "save":
		Save();
		break;
	default:
		$cmd	= "list";
		View();
		break;
}

function View()
{
	set_cookie("SYSTEM_HTTP_REFERER",$_SERVER['REQUEST_URI']);
	$url				= ANotify::parse_URI("id","cmd");
	$url				= "$url&cmd=edit&id=";
?>
	<table Class="yc" cellspacing="1" cellpadding="0" >
       <tr>
	    <th width="40%">栏目名称</th>
	    <th width="10%">类型</th>
        <th width="50%">模板 </th>
      </tr>
	  <tr>
	    <td colspan="3" ><a href="<?=ATHENA_SCRIPT_INDEX?>?Model=<?=$GLOBALS['ATHENA_MODEL']?>&cmd=edit&id=0">系统功能</a></td>
	  </tr>
	  <?=$GLOBALS['warmy_tree']->View($url,false);?>
	  </form>
    </table>
<br>
<?}

function Edit()
{
	//接收处理待编辑内容
	$Id = $_GET['id'] && ereg("^[0-9]+$",$_GET['id']) ? $_GET['id'] : 0 ;
	$list = $GLOBALS['db']->query_row("select id,name,root from `".FUNCTIONTABLE."` where root='$Id' order by idx",MYSQL_ASSOC);
?>
	<table Class="yc" cellspacing="1" cellpadding="0" >
      <tr>
	    <th  colspan="2" >顺序调整</th>
      </tr>
	  <tr>
	    <td>
		<select name="fun" size="16" class="SELECT">
		<?
		while (list(,$value) = each($list))
		{
			print("<option value=\"{$value['id']}\">{$value['name']}</option>");
		}
		?>
		</select>
		</td>
		<td width="50%" style="text-align:center">
		  <input type="button" value="  上移  " onclick="moveFun(1);">
		  <br><br><br>
		  <input type="button" value="  下移  " onclick="moveFun(2);">
		  <input type=hidden name=idx value="">
		</td>
	  </tr>
	  <tr bgcolor="#FFFFFF">
		<td colspan="2" height="50" style="text-align:center">
			<input name="Submit" type="submit" class="button" value="保存" onclick="AppendCmd('save')"/>
	      	<input name="back" type="submit" class="button" value="返回" onclick="AppendCmd('back')"/>
	      	<input type="hidden" name="SYSTEM_HTTP_REFERER" value="<?=get_cookie("SYSTEM_HTTP_REFERER")?>">
		</td></tr>
    </table></td>
  
<br>
<script language="javascript">
function moveFun(cmd)
{
	try
	{
		var idx = document.frmList.fun.selectedIndex;
		if(idx<0)
			return;
		if(cmd==1)
		{
			if (idx == 0)
				return;
			tmp = document.frmList.fun.options[idx-1].value;
			document.frmList.fun.options[idx-1].value = document.frmList.fun.options[idx].value;
			document.frmList.fun.options[idx].value = tmp;
			tmp = document.frmList.fun.options[idx-1].text;
			document.frmList.fun.options[idx-1].text = document.frmList.fun.options[idx].text;
			document.frmList.fun.options[idx].text = tmp;
			document.frmList.fun.selectedIndex = idx-1;
		}
		else
		{
			if(idx>=document.frmList.fun.length-1)
				return;
			tmp = document.frmList.fun.options[idx+1].value;
			document.frmList.fun.options[idx+1].value = document.frmList.fun.options[idx].value;
			document.frmList.fun.options[idx].value = tmp;
			tmp = document.frmList.fun.options[idx+1].text;
			document.frmList.fun.options[idx+1].text = document.frmList.fun.options[idx].text;
			document.frmList.fun.options[idx].text = tmp;
			document.frmList.fun.selectedIndex = idx+1;
		}
	}
	catch(e)
	{
		alert(e.description);
	}
}
function CheckForm()
{
	str = "";
	for(i=0;i<document.frmList.fun.length;i++)
	{
		if(str == "")
			str = document.frmList.fun.options[i].value;
		else
			str += "," + document.frmList.fun.options[i].value;
	}
	document.frmList.idx.value = str;
	document.frmList.submit();
}

</script>
<?
}
function Save()
{
	Add_Op_Log("对功能进行重新排序");
	//接收排序功能
	$idx = $_POST["idx"];
	if (!ereg("^[0-9,]+$",$idx))
		ANotify::halt("请正确选择需要排序的功能点");
	else
		$GLOBALS['warmy_tree']->SaveSort($idx);
	ANotify::halt("重新排序成功！",ATHENA_SCRIPT_INDEX."?Model={$GLOBALS['ATHENA_MODEL']}");
	
?>
<?}?>